//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.MessageRecord;
import com.wsd.smartcarwasher.domain.Pagination;
import com.wsd.smartcarwasher.service.RecordService;
import com.wsd.smartcarwasher.util.Page;
import com.wsd.smartcarwasher.util.PageInfo;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Api(
        tags = {"机器事件记录相关接口"}
)
@RestController
@CrossOrigin
public class RecordController {
    @Resource
    private RecordService recordService;

    public RecordController() {
    }

    @ApiOperation(
            value = "机器事件记录查询",
            notes = "机器事件记录查询",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "machineId",
            value = "机器id",
            required = false,
            dataType = "String"
    ), @ApiImplicitParam(
            name = "orderSn",
            value = "订单id",
            required = false,
            dataType = "String"
    ), @ApiImplicitParam(
            name = "level",
            value = "消息级别",
            required = false,
            dataType = "String"
    ), @ApiImplicitParam(
            name = "eventCode",
            value = "事件码",
            required = false,
            dataType = "String"
    ), @ApiImplicitParam(
            name = "startTime",
            value = "开始事件",
            required = false,
            dataType = "Date"
    ), @ApiImplicitParam(
            name = "endTime",
            value = "结束时间",
            required = false,
            dataType = "Date"
    ), @ApiImplicitParam(
            name = "status",
            value = "处理状态",
            required = false,
            dataType = "Integer"
    )})
    @RequestMapping(
            value = {"/v1/records"},
            method = {RequestMethod.GET},
            produces = {"application/json; charset=utf-8"}
    )
    public ModelMap queryMessageRecord(@RequestParam(value = "machineId", required = false) String machineId, @RequestParam(value = "orderSn", required = false) String orderSn, @RequestParam(value = "level", required = false) String level, @RequestParam(value = "eventCode", required = false) String eventCode, @RequestParam(value = "startTime", required = false) Date startTime, @RequestParam(value = "endTime", required = false) Date endTime, @RequestParam(value = "status", required = false) Integer status,
                                       @RequestParam(value = "online",defaultValue = "1") int online,
                                       @RequestParam(required = false) int Page,
                                       @RequestParam(required = false) int PageSize
                                       ) {

        if(online==0)
        {
            List<MessageRecord> records = this.recordService.querylOfflineMessageRecords(machineId, level, eventCode, startTime, endTime, status,Page,PageSize);
            return ReturnUtil.Success(ResultState.QUERY_MESSAGE_RECORD_SUCCESS.getMsg(), new PageInfo<>(records));
        }

        List<MessageRecord> records = this.recordService.queryMessageRecords(machineId, orderSn, level, eventCode, startTime, endTime, status,Page,PageSize);
        return ReturnUtil.Success(ResultState.QUERY_MESSAGE_RECORD_SUCCESS.getMsg(), new PageInfo<>(records));
    }

    @ApiOperation(
            value = "机器事件记录查询",
            notes = "机器事件记录查询",
            produces = "application/json",
            response = ModelMap.class
    )
    @ApiImplicitParam(
            name = "record",
            value = "告警记录信息，只需要id和status字段",
            required = true,
            dataType = "MessageRecord"
    )
    @RequestMapping(
            value = {"/v1/record"},
            method = {RequestMethod.POST},
            produces = {"application/json; charset=utf-8"}
    )
    public ModelMap updateMessageRecord(@RequestBody MessageRecord record) {
        boolean isSuccess = this.recordService.changeRecordStatus(record);
        if (!isSuccess) {
            ReturnUtil.Error(ResultState.UPDATE_RECORD_STATUS_FAIL.getMsg());
        }

        return ReturnUtil.Success(ResultState.UPDATE_RECORD_STATUS_SUCCESS.getMsg());
    }
}
